package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;

import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class ProgressSlice extends AbilitySlice implements Component.ClickedListener {
    private Image back_button;
    private Text text_title;
    private Button button1, button2, button3;
    private Component component1, component2, component3;
    private AnimatorProperty animator1, animator2, animator3;
    private boolean flag1, flag2, flag3;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_progress);
        initView();
        initAnimation();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Progress bars")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Progress bars")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Progress bars", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Progress bars", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Progress bars", 1);
            pf.flushSync();
        }
    }

    private void initAnimation() {
        //动画
        animator1 = component1.createAnimatorProperty();
        animator2 = component2.createAnimatorProperty();
        animator3 = component3.createAnimatorProperty();
        //获取手机屏幕宽度displayAttributes.width
        DisplayManager displayManager = DisplayManager.getInstance();
        Display display = displayManager.getDefaultDisplay(this).get();
        DisplayAttributes displayAttributes = display.getAttributes();
        int windowWidth = displayAttributes.width;

        animator1.moveFromX(-1080f).moveByX(1080.0f).setDuration(1600).setLoopedCount(-1).start();
        animator2.moveFromX(-1080f).moveByX(1080.0f).setDuration(3000).setLoopedCount(-1).start();
        animator3.moveFromX(1080f).moveByX(-1080.0f).setDuration(1600).setLoopedCount(-1).start();
    }

    private void initView() {
        component1 = findComponentById(Id_component1);
        component2 = findComponentById(Id_component2);
        component3 = findComponentById(Id_component3);
        button1 = (Button) findComponentById(Id_button1);
        button2 = (Button) findComponentById(Id_button2);
        button3 = (Button) findComponentById(Id_button3);
        button1.setClickedListener(this);
        button2.setClickedListener(this);
        button3.setClickedListener(this);
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Progress bars");
        back_button.setClickedListener(this);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        animator1.reset();
        animator2.reset();
        animator3.reset();
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
            animator1.reset();
            animator2.reset();
            animator3.reset();
        } else if (component.getId() == Id_button1) {
            component1.setVisibility(flag1 ? Component.VISIBLE : Component.INVISIBLE);
            flag1 = !flag1;
        } else if (component.getId() == Id_button2) {
            component2.setVisibility(flag2 ? Component.VISIBLE : Component.INVISIBLE);
            flag2 = !flag2;
        } else if (component.getId() == Id_button3) {
            component3.setVisibility(flag3 ? Component.VISIBLE : Component.INVISIBLE);
            flag3 = !flag3;
        }
    }
}
